/**
 * related to myList.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-04
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");

var hashdata = mm("do_HashData");
var listdata = mm("do_ListData");
var listview = ui("slideListView");
listview.bindItems(listdata);
var rootview = ui("$"); 
var circleUrl = require("url/circle");
var httpSilence = require("helper/httpSilence");
var open = require("helper/open");
var userUtil = require("helper/user");
/****************处理页面-头部-begin*****************/
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "管理群聊成员"
})
header.refreshData();

//右上角-
var headerRight = ui(addheader + ".headerRight");
// 右上角添加+图标
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/more.png";
imgNext.visible = true;

var menus=[{"template" : 0,"menuIcon":"source://image/menu/useradd.png","menuName" : "邀请新成员","type":"addMember"}];
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menu.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	popmenuf.fire("popmenuSJ", menus);
});
/****************处理页面-头部-end*****************/
var addGuide = ui("$").add("guide", "source://view/circle/gchat/member/manage/guide.ui", 0, 0);

//andriod返回键
page.on("back", function() {
    app.closePage();
});

var circleId = null;
page.on("loaded", function(){
	circleId = page.getData().circleId;
	refreshAllData();
});

page.on("kick_member", function() {
	refreshAllData();
});

page.on("reflash_member", function() {
	refreshAllData();
});

page.on("result", function(data){
	if (data.kick_member || data.member_update) {
		refreshAllData();
    }
});

// 刷新数据
function refreshAllData(){
	httpSilence.post(circleUrl.url.member.members4manageCircle, {circleId:circleId}, function(data) {
		listdata.removeAll();
		var circleMembers = data.members;
		if(circleMembers){
			for(var i=0;i<circleMembers.length;i++){
				if(circleMembers[i].type =="1"){
					continue;
				}
				circleMembers[i].template=0;
				circleMembers[i].rightTemplate=1;
			}
			listdata.addData(circleMembers);
			listview.refreshItems();//刷新item数据
		}
	})
}

//点击一个成员
listview.on("touch", function(data){
	var member = listdata.getOne(data);
	var id = member.id;
	if(id != null){
		open.open("source://view/circle/gchat/member/manage/detail.ui",{id:id});
	}
});

//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		refreshAllData();
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		refreshAllData();
		listview.rebound();//复位【一定要做】
	}
});

//点击菜单选项
popmenuf.on("popmenuItem", function(data) {
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	
	if(actionType=="addMember"){//邀请新成员
		if(userUtil.loginToken()===""){
			nf.toast("您还没有登录哦.");
			return;
		}
		open.open("source://view/circle/gchat/member/invite/invite2gchat.ui",{"circleId":circleId});
		
	}
	popmenuf.fire("popmenuClose");
});